<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>组件通信-子传父（自定义事件）</title>
  </head>
  <body>
    <!-- 父组件模板 -->
    <div id="app">
      <cpn @itemclick="cpnClick"></cpn>
    </div>
    <script src="../vue.js"></script>

    <!-- 子组件模板 -->
    <template id="cpn">
      <div>
        <button v-for="item in categories" @click="btnClick(item)">
          {{item.name}}
        </button>
      </div>
    </template>
    <script>
      //子组件
      const cpn = {
        template: "#cpn",
        data() {
          return {
            categories: [
              { id: "a", name: "热门推荐" },
              { id: "b", name: "手机数码" },
              { id: "c", name: "家用家电" },
              { id: "d", name: "时尚饰品" },
            ],
          };
        },
        methods: {
          btnClick(item) {
            this.$emit("itemclick", item);
          },
        },
      };
      //父组件
      const app = new Vue({
        el: "#app",
        data: {
          message: "hello",
        },
        components: {
          cpn,
        },
        methods: {
          cpnClick(item) {
            console.log("cpnClick", item);
          },
        },
      });
    </script>
  </body>
</html>
